/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("Mds_ProductionTask",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using DGZImp.MdsProject.DomainModels;
using DGZImp.MdsProject.IServices;
using DGZImp.Core.Filters;
using DGZImp.Core.Utilities.Response;
using DGZImp.MdsProject.Devices;

namespace DGZImp.MdsProject.Controllers
{
    public partial class Mds_ProductionTaskController
    {
        private readonly IMds_ProductionTaskService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public Mds_ProductionTaskController(
            IMds_ProductionTaskService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 手动启动物流任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [HttpPost("StartProductionTask")]
        public async Task<ApiResponse<string>> StartProductionTask([FromQuery] long? taskId)
        {
            await Service.StartProductionTask(taskId);
            return ApiRes.Success("");
        }

        /// <summary>
        /// 手动完成任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [HttpPost("FinishProductionTask")]
        public async Task<ApiResponse<string>> FinishProductionTask([FromBody] FinishProductionTaskReq req)
        {
            await Service.FinishProductionTask(req.taskId, new ProductorData{ FinishedQuantity = req.completionQuantity ?? 0}, req.completionReason, true);
            return ApiRes.Success("");
        }

        /// <summary>
        /// 自动下发任务，并处理任务完成数据
        /// </summary>
        /// <returns></returns>
        [ApiTask]
        [HttpPost("AutoSendAndCompleteProductionTask")]
        public async Task<ApiResponse<string>> AutoSendAndCompleteProductionTask()
        {
            await Service.AutoCompleteProductionTask();
            await Service.AutoSendProductionTask();
            return ApiRes.Success("");
        }

        /// <summary>
        /// aps下发首道序创建Mds生产任务
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost("CreateMdsProductionTask")]
        public async Task<ApiResponse<bool>> CreateMdsProductionTask([FromBody] Aps_DistributedTask item)
        {
            var r = await _service.CreateMdsProductionTask(item);
            return ApiRes.Success(r);
        }
    }

    /// <summary>
    /// 手动完成请求参数
    /// </summary>
    public class FinishProductionTaskReq
    {
        /// <summary>
        /// 任务Id
        /// </summary>
        public long? taskId { get; set; }

        /// <summary>
        /// 完成数量
        /// </summary>
        public decimal? completionQuantity { get; set; } = null;

        /// <summary>
        /// 完成原因
        /// </summary>
        public string completionReason { get; set; } = null;
    }
}
